<?php
/**
 * Created by PhpStorm.
 * User: 10838
 * Date: 2017/10/27
 * Time: 11:07
 */
//会员等级


namespace app\admin\controller;


use app\admin\model\PrivilegeModel;
use app\admin\model\ViplevelModel;
use think\Loader;

class Viplevel extends Base{


    public function index(){

        $data = ViplevelModel::order('level','desc')->select();
        foreach($data as $k=>$v){
           $data[$k]->privileges = PrivilegeModel::all($v->privilege);
        }
        $this->assign('data',$data);
        return view('index');
    }

    public function add(){
        //权益
        $privilege = PrivilegeModel::all();
        $this->assign('privilege',$privilege);
        return view('add');
    }
    public function add_from(){
        if(request()->isPost()){


            $is_cunzai = ViplevelModel::where('level',$_POST['level'])->find();
            if($is_cunzai){
                $this->error('等级已存在');
            }
            $is_title = ViplevelModel::where('title',$_POST['title'])->find();
            if($is_title){
                $this->error("等级名已存在");
            }
            if(isset($_POST['privilege'])){
                $privilege = implode(',',$_POST['privilege']);
            }else{
                $privilege = '';
            }
            $validate = Loader::validate('Viplevel');
            if(!$validate->check($_POST)){
                $this->error($validate->getError());
            }else{
                if($_POST['jifen_up']>=$_POST['jifen_down']){
                    $this->error('起始积分必须小于终止积分');
                }else{
                   $re = ViplevelModel::create([
                        'level'=>$_POST['level'],
                        'title'=>$_POST['title'],
                        'range_up'=>$_POST['jifen_up'],
                       'range_down'=>$_POST['jifen_down'],
                       'privilege'=>$privilege,
                        "time"=>time()
                    ]);

                    if($re){
                        $this->success('添加成功',"admin/viplevel/index");
                    }else{
                        $this->error("添加失败");

                    }
                }
            }
        }else{
            $this->error('非法操作!');
        }
    }
    public function edit($id){
        $data = ViplevelModel::get($id);
        //权益
        $privilege = PrivilegeModel::all();
        $privileges = explode(',',$data->privilege);

        $this->assign('privileges',$privileges);
        $this->assign('privilege',$privilege);
        $this->assign('data',$data);
        return view('edit');
    }
    public function edit_from(){
        if(request()->isPost()){
            $validate = Loader::validate('Viplevel');
            if(!$validate->check($_POST)){
                $this->error($validate->getError());
            }else{
                $re = ViplevelModel::where('id',$_POST['id'])
                    ->update([
                        'level'=>$_POST['level'],
                        'title'=>$_POST['title'],
                        'range_up'=>$_POST['jifen_up'],
                        'range_down'=>$_POST['jifen_down'],
                        'privilege' => implode(',',$_POST['privilege'])
                    ]);
                if(isset($re)){
                    $this->success('修改成功！',"admin/viplevel/index");
                }else{
                    $this->error("修改失败");
                }
            }
        }else{
            $this->error('非法操作！');
        }
    }

        public function del(){
            $model = ViplevelModel::get($_POST['help_id']);
            $re =$model->delete();
            var_dump($re);

        }



}